package com.bookstore.back.controller;

import com.bookstore.back.common.Result;
import com.bookstore.back.dto.LoginDTO;
import com.bookstore.back.dto.RegisterDTO;
import com.bookstore.back.service.AuthService;
import com.bookstore.back.vo.LoginVO;
import com.bookstore.back.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 认证控制器
 * 
 * @author 程序猿_Ti
 * @since 2025-07-16
 */
@Slf4j
@RestController
@RequestMapping("/auth")
@Validated
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginVO> login(@Valid @RequestBody LoginDTO loginDTO) {
        try {
            log.info("用户登录请求：{}", loginDTO.getUsername());
            LoginVO loginVO = authService.login(loginDTO);
            return Result.success("登录成功", loginVO);
        } catch (Exception e) {
            log.error("用户登录失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Map<String, Object>> register(@Valid @RequestBody RegisterDTO registerDTO) {
        try {
            log.info("用户注册请求：{}", registerDTO.getUsername());
            UserVO userVO = authService.register(registerDTO);
            
            // 构建前端期望的响应格式
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "注册成功");
            response.put("user", userVO);
            
            return Result.success("注册成功", response);
        } catch (Exception e) {
            log.error("用户注册失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 检查用户名是否可用
     */
    @GetMapping("/check-username/{username}")
    public Result<Map<String, Object>> checkUsername(@PathVariable String username) {
        try {
            boolean available = authService.checkUsername(username);
            Map<String, Object> response = new HashMap<>();
            response.put("available", available);
            response.put("message", available ? "用户名可用" : "用户名已存在");
            return Result.success(response);
        } catch (Exception e) {
            log.error("检查用户名失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 检查邮箱是否可用
     */
    @GetMapping("/check-email/{email}")
    public Result<Map<String, Object>> checkEmail(@PathVariable String email) {
        try {
            boolean available = authService.checkEmail(email);
            Map<String, Object> response = new HashMap<>();
            response.put("available", available);
            response.put("message", available ? "邮箱可用" : "邮箱已被注册");
            return Result.success(response);
        } catch (Exception e) {
            log.error("检查邮箱失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 检查手机号是否可用
     */
    @GetMapping("/check-phone/{phone}")
    public Result<Map<String, Object>> checkPhone(@PathVariable String phone) {
        try {
            boolean available = authService.checkPhone(phone);
            Map<String, Object> response = new HashMap<>();
            response.put("available", available);
            response.put("message", available ? "手机号可用" : "手机号已被注册");
            return Result.success(response);
        } catch (Exception e) {
            log.error("检查手机号失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

}
